package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.AgedmallGoodsSaleInfoMapper;
import org.linlinjava.litemall.db.dao.app.AppGoodsSaleInfoMapper;
import org.linlinjava.litemall.db.domain.AgedmallGoodsSaleInfo;
import org.linlinjava.litemall.db.domain.AgedmallGoodsSaleInfoExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class AgedmallGoodsSaleInfoService {
    @Resource
    private AgedmallGoodsSaleInfoMapper agedmallGoodsSaleInfoMapper;
    @Resource
    private AppGoodsSaleInfoMapper appGoodsSaleInfoMapper;

    public AgedmallGoodsSaleInfo getSaleInfoByGoodsId(Integer goodsId) {
        AgedmallGoodsSaleInfoExample saleInfoExample = new AgedmallGoodsSaleInfoExample();
        saleInfoExample.or().andGoodsIdEqualTo(goodsId);
        return agedmallGoodsSaleInfoMapper.selectOneByExample(saleInfoExample);
    }

    public void addSaleCountByGoodsId(Integer goodsId) {
        AgedmallGoodsSaleInfo agedmallGoodsSaleInfo = getSaleInfoByGoodsId(goodsId);
        if (null == agedmallGoodsSaleInfo) {
            agedmallGoodsSaleInfo = new AgedmallGoodsSaleInfo();
            agedmallGoodsSaleInfo.setGoodsId(goodsId);
            agedmallGoodsSaleInfo.setSaleCount(1);
            add(agedmallGoodsSaleInfo);
        } else {
            appGoodsSaleInfoMapper.addSaleCountByGoodsId(goodsId);
        }
    }

    public void add(AgedmallGoodsSaleInfo agedmallGoodsSaleInfo) {
        agedmallGoodsSaleInfo.setAddTime(LocalDateTime.now());
        agedmallGoodsSaleInfo.setUpdateTime(LocalDateTime.now());
        agedmallGoodsSaleInfoMapper.insertSelective(agedmallGoodsSaleInfo);
    }
}
